package com.dmbjz.webdemo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;
import java.util.concurrent.TimeUnit;

/** MVC配置类 */
@Configuration
public class MyWebConfig implements WebMvcConfigurer  {


    /** 指定静态资源映射 */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        //继承原有定义配置
        WebMvcConfigurer.super.addResourceHandlers(registry);

        //添加自定义配置（访问 localhost:8080/static/xxx 的静态资源会自动去 /a、/b 文件夹下找文件）
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/a/","classpath:/b/")
                .setCacheControl(CacheControl.maxAge(1000, TimeUnit.SECONDS));


    }

    //配置一个能把对象转为 yaml 的 messageConverter
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MyYamlHttpMessageConverter());
    }


}
